/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.googlecode.microanalyst.designer.model.commands;

import java.util.List;

import org.eclipse.gef.commands.Command;

import com.googlecode.microanalyst.designer.i18n.DesignerMessages;
import com.googlecode.microanalyst.designer.model.AnalysisConnection;
import com.googlecode.microanalyst.designer.model.AnalysisElement;

public class ConnectionCommand extends Command {

	protected AnalysisElement oldSource;
	protected String oldSourceTerminal;
	protected AnalysisElement oldTarget;
	protected String oldTargetTerminal;
	protected AnalysisElement source;
	protected String sourceTerminal;
	protected AnalysisElement target;
	protected String targetTerminal;
	protected AnalysisConnection connection;

	public ConnectionCommand() {
		super(DesignerMessages.ConnectionCommand_Label);
	}

	public boolean canExecute() {
		if (target != null) {
			List<AnalysisConnection> conns = target.getConnections();
			for (AnalysisConnection conn : conns) {
				if (targetTerminal != null && conn.getTargetTerminal() != null)
					if (conn.getTargetTerminal().equals(targetTerminal)
							&& conn.getTarget().equals(target))
						return false;
			}
		}
		return true;
	}

	public void execute() {
		if (source != null) {
			connection.detachSource();
			connection.setSource(source);
			connection.setSourceTerminal(sourceTerminal);
			connection.attachSource();
		}
		if (target != null) {
			connection.detachTarget();
			connection.setTarget(target);
			connection.setTargetTerminal(targetTerminal);
			connection.attachTarget();
		}
		if (source == null && target == null) {
			connection.detachSource();
			connection.detachTarget();
			connection.setTarget(null);
			connection.setSource(null);
		}
	}

	public String getLabel() {
		return DesignerMessages.ConnectionCommand_Description;
	}

	public AnalysisElement getSource() {
		return source;
	}

	public java.lang.String getSourceTerminal() {
		return sourceTerminal;
	}

	public AnalysisElement getTarget() {
		return target;
	}

	public String getTargetTerminal() {
		return targetTerminal;
	}

	public AnalysisConnection getConnection() {
		return connection;
	}

	public void redo() {
		execute();
	}

	public void setSource(AnalysisElement newSource) {
		source = newSource;
	}

	public void setSourceTerminal(String newSourceTerminal) {
		sourceTerminal = newSourceTerminal;
	}

	public void setTarget(AnalysisElement newTarget) {
		target = newTarget;
	}

	public void setTargetTerminal(String newTargetTerminal) {
		targetTerminal = newTargetTerminal;
	}

	public void setConnection(AnalysisConnection w) {
		connection = w;
		oldSource = w.getSource();
		oldTarget = w.getTarget();
		oldSourceTerminal = w.getSourceTerminal();
		oldTargetTerminal = w.getTargetTerminal();
	}

	public void undo() {
		source = connection.getSource();
		target = connection.getTarget();
		sourceTerminal = connection.getSourceTerminal();
		targetTerminal = connection.getTargetTerminal();

		connection.detachSource();
		connection.detachTarget();

		connection.setSource(oldSource);
		connection.setTarget(oldTarget);
		connection.setSourceTerminal(oldSourceTerminal);
		connection.setTargetTerminal(oldTargetTerminal);

		connection.attachSource();
		connection.attachTarget();
	}

}
